package com.edu.test.dbf;

import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @作者 five-five
 * @创建时间 2020/12/14
 */
public class DbfMain {

    public static void sayHello(){
        System.out.println("Hello");
    }
    public static void main(String[] args) {
        DbfMain dbfMain = new DbfMain();
        List<Map<String, Object>> maps = dbfMain.readDbf("E:\\浙江\\T_KSJL.dbf", "GBK");
//        List<List<Object>> lists = dbfMain.readDbfToList("E:\\浙江\\T_KSJL.dbf", "GBK");
        maps.forEach(System.out::println);
        Map<String, Object> map = maps.get(0);
        System.out.println(map);
//        lists.forEach(System.out::println);
    }


    /**
     * 取出dbf文件内容
     *
     * @param path
     * @param charsetName
     * @return
     */
    public List<Map<String, Object>> readDbf(String path, String charsetName) {
        List<Map<String, Object>> maps = new ArrayList<>();
        FileInputStream fis = null;
        try {
            //设置文件编码格式
            fis = new FileInputStream(path);
            DBFReader dbfReader = new DBFReader(fis);
            Charset charset = Charset.forName(charsetName);
            dbfReader.setCharset(charset);
//            Charset charset1 = dbfReader.getCharset();
//            System.out.println(charset1);
            int fieldCount = dbfReader.getFieldCount();
            //取出字段信息
//            for (int i = 0; i < fieldCount; i++) {
//                DBFField field = dbfReader.getField(i);
//                System.out.print(field.getName()+"\t");
//            }
//            System.out.println();
            //逐句取出path文件中的记录
            Object[] rowValues = dbfReader.nextRecord();
//            System.out.println(rowValues.length);
            while ((rowValues = dbfReader.nextRecord()) != null && rowValues.length != 0) {
                int index = 0;
                Map<String, Object> map = new LinkedHashMap<>();
                for (int i = 0; i < rowValues.length; i++) {
                    DBFField field = dbfReader.getField(index);
                    map.put(dbfReader.getField(index).getName(), rowValues[i]);
//                    System.out.print(field.getName()+":::"+rowValues[i]+ "\t");
                    index++;
                }
                maps.add(map);
//                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return maps;
    }

    /**
     * (行数一致)按照原内容取出dbf文件内容
     *
     * @param path
     * @param charsetName
     * @return
     */
    public List<List<Object>> readDbfToList(String path, String charsetName) {
        List<List<Object>> lists = new ArrayList<>();
        //首列
        List<Object> titles = new ArrayList<>();
        FileInputStream fis = null;
        //设置文件编码格式
        try {
            fis = new FileInputStream(path);
            DBFReader dbfReader = new DBFReader(fis);
            Charset charset = Charset.forName(charsetName);
            dbfReader.setCharset(charset);
            int fieldCount = dbfReader.getFieldCount();
            for (int i = 0; i < fieldCount; i++) {
                DBFField field = dbfReader.getField(i);
                titles.add(field.getName());
//                System.out.print(field.getName()+"\t");
            }
            lists.add(titles);
//            System.out.println();
            Object[] objs = null;
            while ((objs = dbfReader.nextRecord()) != null && objs.length != 0) {
                List<Object> temps = new ArrayList<>();
                for (Object obj : objs) {
                    temps.add(obj);
//                    System.out.print(obj.toString()+"\t");
                }
                lists.add(temps);
//                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return lists;
    }
}
